package com.woniuxy.movice.handler;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.woniuxy.movice.dto.Page;
import com.woniuxy.movice.entity.Movie;
import com.woniuxy.movice.service.MovieService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;

@Data
@Controller
@RequestMapping("/movie")
@Api("电影信息")
public class MovieController {
	
	@Resource
	private MovieService movieService;
	
	@ApiOperation(value="根据条目展示电影",notes="传入当前条目")
	@RequestMapping(value="/showMovieByCount",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> showMovieByCount(Page<Movie> page) {
		Map<String, Object> result = new HashMap<>();
		int total = movieService.findMovieNum();
		int index = (page.getCurrentPage() - 1) * 6;
		int totalPage = total % 6 == 0 ? total / 6 : total / 6 + 1;
		page.setIndex(index);
		page.setCount(6);
		List<Movie> movies = movieService.findMovieByPage(page);
		result.put("totalPage", totalPage);
		result.put("movies", movies);
		return result;
	}
	
	@ApiOperation(value="分页展示电影",notes="传入当前页码")
	@RequestMapping(value="/showMovieByPage",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> showMovieByPage(Page<Movie> page) {
		Map<String, Object> result = new HashMap<>();
		int total = movieService.findMovieNum();
		int index = (page.getCurrentPage() - 1) * 9;
		int totalPage = total % 9 == 0 ? total / 9 : total / 9 + 1;
		page.setIndex(index);
		page.setCount(9);
		List<Movie> movies = movieService.findMovieByPage(page);
		result.put("totalPage", totalPage);
		result.put("movies", movies);
		result.put("total", total);
		return result;
	}
	
	@ApiOperation(value="通过类型分页展示电影",notes="传入当前页码、类型")
	@RequestMapping(value="/showMovieByType",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> showMovieByType(Page<Movie> page) {
		Map<String, Object> result = new HashMap<>();
		int total = movieService.findMovieNumByType("%"+page.getMtype()+"%");
		int index = (page.getCurrentPage() - 1) * 9;
		int totalPage = total % 9 == 0 ? total / 9 : total / 9 + 1;
		page.setIndex(index);
		page.setCount(9);
		page.setMtype("%"+page.getMtype()+"%");
		List<Movie> movies = movieService.findMovieByType(page);
		result.put("totalPage", totalPage);
		result.put("movies", movies);
		result.put("total", total);
		return result;
	}
	
	@ApiOperation(value="展示10部电影")
	@RequestMapping(value="/showTenMovie",method=RequestMethod.GET)
	@ResponseBody
	public List<Movie> showTenMovie(){
		return movieService.showTenMovie();
	}
	
	@ApiOperation(value="通过电影id查询电影信息",notes="传入电影id")
	@RequestMapping(value="/showMovieById",method=RequestMethod.POST)
	@ResponseBody
	public Movie showMovieById(int id){
		return movieService.showMovieById(id);
	}
	
}
